<?php

namespace app\admin\controller;

use app\common\controller\Adminbase;
use think\Request;

class Log extends Adminbase
{
    public function admin_log(){
        $info = db('logs')->order('id asc')->select();
        $field = [
            ['field'=>'id','title'=>'id'],
            ['field'=>'routes','title'=>'节点地址'],
            ['field'=>'ip','title'=>'触发IP'],
            ['field'=>'uid','title'=>'用户信息'],
            ['field'=>'describes','title'=>'事件详情'],
            ['field'=>'access','title'=>'触发方式'],
            ['field'=>'create_at','title'=>'创建时间']
        ];
        $operate = $this->list_page;
        $operate += ['bar_add'=>1];
        $operate += ['r_bar_edit'=>1];
        $operate['delete'] = 'admin/log/admin_log_delete';
        return view('public/list',['filed'=>$field,'info'=>json_encode($info,JSON_UNESCAPED_UNICODE),'operate'=>$operate]);
    }
    public function admin_log_delete(Request $request){
        $id = $request->param('id');
        if(!is_array($id)){
            if(!is_numeric($id)){
                return json(['code'=>1,'msg'=>'数据错误']);
            }else{
                $id = explode(',',$id);
            }
        }
        if(is_array($id)){
            foreach($id as $k=>$val){
                $old = db('logs')->where(['id'=>$val])->find();
                if(!$old){
                    return ['code'=>1,'msg'=>'存在非数据库数据，请核查'];
                }
            }
            $res = db('logs')->delete($id);
            if($res){
                return json(['code'=>200,'msg'=>'删除成功']);
            }else{
                return json(['code'=>1,'msg'=>'删除失败']);
            }
        }else{
            return json(['code'=>1,'msg'=>'数据错误']);
        }
    }
}